package cn.webrx.bs.db;

import java.security.MessageDigest;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-05 14:48
 */
public class DbUtil {

    /**
     * <p>功能：检查账号登录是否成功</p>
     * @param a
     * @param p
     * @param t
     * @param pass
     * @return
     */
    public boolean isLogin(String a,String p,String t,String pass){
        boolean f = false;
        if(pass.equals(mm(a+p+t))) f = true;
        return f;
    }

    /**
     * 返回：32密文字符串
     * @param p
     * @return
     */
    public String mm(String p){
        StringBuilder pp = new StringBuilder();
        String pmd5 = md(p,"md5");
        String psha1 = md(p,"sha1");
        pp.append(pmd5.substring(0,5));
        pp.append(psha1.substring(1,6));
        pp.append(pmd5.substring(20,25));
        pp.append(psha1.substring(21,26));
        pp.append(pmd5.substring(29));
        pp.append(psha1.substring(31));
        return pp.toString();
    }

    /**
     * <p>功能：加密md5 加密sha1</p>
     * @param str
     * @param type
     * @return
     */
    public String md(String str,String type) {
        StringBuffer pass = new StringBuffer(32);
        try {
            MessageDigest m = MessageDigest.getInstance(type);
            m.update(str.getBytes());
            for (byte b : m.digest()) {
                String s = String.format("%x", b);
                pass.append(s.length() == 2 ? s : "0" + s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pass.toString();
    }
}
